<?php
// +----------------------------------------------------------------------
// | 那些街购物分享网站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://fanwe.com All rights reserved.
// +----------------------------------------------------------------------

class UserBase
{
	public function getConfig($type)
	{
		static $configs = NULL;
		if($configs === NULL)
		{
			global $_FANWE;
			if(!isset($_FANWE['cache']['logins']))
				FanweService::instance()->cache->loadCache('logins');
			$configs = $_FANWE['cache']['logins'];
		}
		return $configs[$type];
	}

	public function getUserByTypeKeyId($type,$key_id)
	{
		$sql = 'SELECT u.uid,u.password,u.status FROM '.FDB::table('user_bind').' as ub
				INNER JOIN '.FDB::table('user').' as u ON u.uid = ub.uid
						WHERE ub.type = \''.$type.'\' AND ub.keyid = \''.$key_id.'\'';

		return FDB::fetchFirst($sql);
	}

	public function jumpUserBindReg($data,$user_name)
	{
		do
		{
			$max_count = FDB::resultFirst('SELECT COUNT(*) FROM '.FDB::table("user")." WHERE user_name = '".$user_name."'");
			if($max_count > 0)
				$user_name = $user_name.'_'.random(3);
		}
		while($max_count > 0);
		$data['user_name'] = $user_name;
		$data = serialize($data);
		fSetCookie('bind_user_info',authcode($data,'ENCODE'));
		//fHeader("location:".FU('user/bind'));
		$this->saveUserBind($user_name);
	}

	public  function saveUserBind($user_name)
	{
		global $_FANWE;

		if(empty($_FANWE['cookie']['bind_user_info']))
			fHeader("location: ".FU('user/register'));
		
		$bind_info = unserialize(authcode($_FANWE['cookie']['bind_user_info'], 'DECODE'));
		if(empty($bind_info))
			fHeader("location: ".FU('user/register'));
		//================add by chenfq 2011-10-14 =======================
		$user_field = $_FANWE['setting']['integrate_field_id'];
		$integrate_id = FS("Integrate")->addUser($user_name,"","");
		if ($integrate_id < 0)
		{
			$info = FS("Integrate")->getInfo();
			showError('注册失败',$info,-1);
		};
		//================add by chenfq 2011-10-14=======================



		$user = array(
				'email' => "",
				'user_name' => $user_name,
				'password'  => md5(""),
				'status'    => 1,
				'email_status' => 0,
				'avatar' => 0,
				'gid' => 7,
				'invite_id' => FS('User')->getReferrals(),
				'reg_time' => TIME_UTC,
				$user_field => $integrate_id,
		);

		$uid = FDB::insert('user',$user,true);
		if($uid > 0)
		{
			$_FANWE['uid'] = $uid;
			FDB::insert('user_count',array('uid' => $uid));

			if($user['invite_id'] > 0)
				FS('User')->insertReferral($uid,$user['invite_id'],$user['user_name']);

			FS("User")->updateUserScore($uid,'user','register');
			unset($user);

			$user_match = array(
					'uid' => $uid,
					'user_name' => FS("Words")->segmentToUnicode($user['user_name']),
			);
			FDB::insert('user_match',$user_match);
			unset($user_match);

			$user_profile = array(
					'uid' => $uid,
					'gender' => 0,
			);
			FDB::insert('user_profile',$user_profile);
			unset($user_profile);

			$user_status = array(
					'uid' => $uid,
					'reg_ip' => $_FANWE['client_ip'],
					'last_ip' => $_FANWE['client_ip'],
					'last_time' => TIME_UTC,
					'last_activity' => TIME_UTC,
			);
			FDB::insert('user_status',$user_status);

			$user = array(
					'uid'=>$uid,
					'password'=>md5(""),
			);

			fSetCookie('last_request', authcode(TIME_UTC - 10, 'ENCODE'), TIME_UTC + 816400, 1, true);
			FS('User')->setSession($user);

			$syslogin_js = FS("Integrate")->synLogin($integrate_id);//js 需要在前台执行 add by chenfq 2011-10-15
			if (!empty($syslogin_js))
				fSetCookie("dynamic_script",$syslogin_js);

			require_once FANWE_ROOT."core/class/user/".$bind_info['type'].".class.php";
			$class = ucfirst($bind_info['type']).'User';
			$class = new $class();
			$class->bindByData($bind_info);
			fHeader("location:".FU('u/index'));
		}
		else
		{
			showError('注册失败',lang('user','register_error'),-1);
		}
	}
}
?>